package vn.conghuy.talkwithme.responsor.responsor;

import vn.conghuy.talkwithme.R;
import vn.conghuy.talkwithme.data.sqlmodel.SqlData;
import vn.conghuy.talkwithme.responsor.action.EmailAction;
import vn.conghuy.talkwithme.utils.Parser;
import vn.conghuy.talkwithme.utils.StringWrapper;
import vn.conghuy.talkwithme.utils.Utils;
import android.content.Context;
import android.content.Intent;

/**
 * @author user
 *
 */
public class EmailResponsor extends Responsor {
	
	private static final String TO_KEYWORD = "to";
	
	private static final int RELATIVE_ADD_INFO = 1;
	private static final int RELATIVE_ASK_ADD_INFO = 2;
	
	String title;
	String content;
	String email;
	int parseResult;
	
	public EmailResponsor(Context context) {
		super(context);
	}
	
	@Override
	public String response(String request) {
		
		if(Utils.isExistStringInArray(SqlData.stopWords, request)){
			relativeLastResponse = RELATIVE_NONE;
			return Utils.getRandomString(SqlData.taskStop);
		}
		
		switch (relativeLastResponse) {
		case RELATIVE_NONE:
			
			StringWrapper emailVerbWrapper = new StringWrapper();
			StringWrapper emailKeywordWrapper = new StringWrapper();
			
			if(Utils.isExistElemInArrayIsWordOfStr(SqlData.emailVerbs, request, emailVerbWrapper)
					&& Utils.isExistElemInArrayIsWordOfStr(SqlData.emailKeywords, request, emailKeywordWrapper)){
				
				parseRequest(request, emailVerbWrapper.getStr());
				String responseStr = checkInfoAndSendIfEnoughAllInfo();
				return responseStr;
			}
			break;

		case RELATIVE_ASK_ADD_INFO:
			//check if tell "no", or "send now", cancel this task.
			if (Utils.isExistElemInArrayIsWordOfStr(SqlData.callNoWords, request, null)
					|| Utils.isExistElemInArrayIsWordOfStr(SqlData.emailSendNow, request, null)){
				String responseStr = checkInfoAndSendIfExistAnyInfo();
				return responseStr;
			}
			else{
				parseRequest(request, null);
				parseContentIfHaveNotAnyKeywordOrOnlyHaveContentKeyword(request);
				String responseStr = checkInfoAndSendIfExistAnyInfo();
				return responseStr;
			}
		case RELATIVE_ADD_INFO:
			parseRequest(request, null);
			parseContentIfHaveNotAnyKeywordOrOnlyHaveContentKeyword(request);
			String responseStr = checkInfoAndSendIfEnoughAllInfo();
			return responseStr;
		default:
			break;
		}
		return super.response(request);
	}
	/**
	 * This function parse request and set value for variable like content, address, parseResult
	 * @param request : request to parse
	 */
	void parseRequest(String request, String emailVerb){
		String emailPhrase;
		
		int emailIndex = addressIndex(request, emailVerb);
		
		StringWrapper contentKeywordWrapper = new StringWrapper();
		int contentIndex = contentIndex(request, contentKeywordWrapper);
		String contentKeyword = contentKeywordWrapper.getStr();
		
		StringWrapper titleKeywordWrapper = new StringWrapper();
		int titleIndex = titleIndex(request, titleKeywordWrapper);
		String titleKeyword = titleKeywordWrapper.getStr();
		
		// set title
		if(titleIndex >= 0){
			int nextIndex = Utils.firstGreaterThanNumber(titleIndex, contentIndex, emailIndex);
			if(nextIndex != -1)
				title = request.substring(titleIndex + titleKeyword.length() + 1, nextIndex);
			else title = request.substring(titleIndex + titleKeyword.length() + 1);
			title = Utils.standardizePhrase(title);
		}
		
		if(contentIndex >= 0){
			int nextIndex = Utils.firstGreaterThanNumber(contentIndex, titleIndex, emailIndex);
			if(nextIndex != -1)
				content = request.substring(contentIndex + contentKeyword.length() + 1, nextIndex);
			else content = request.substring(contentIndex + contentKeyword.length() + 1);
			content = Utils.standardizePhrase(content);
		}
		
		if(emailIndex >= 0){
			int nextIndex = Utils.firstGreaterThanNumber(emailIndex, titleIndex, contentIndex);
			if(nextIndex != -1)
				emailPhrase = request.substring(emailIndex + TO_KEYWORD.length() + 1, nextIndex);
			else emailPhrase = request.substring(emailIndex + TO_KEYWORD.length() + 1);
			emailPhrase = Utils.standardizePhrase(emailPhrase);
			parseEmail(emailPhrase);
		}
		
		else if(contentIndex < 0) content = request;
	}
	
	boolean parseEmail(String emailPhrase){
		StringWrapper emailWrapper = new StringWrapper();
		boolean result = Parser.parseEmail(emailPhrase, emailWrapper);
		email = emailWrapper.getStr();
		return result;
	}
	
	int addressIndex(String request, String emailVerb){
		int indexOfVerb = -1;
		if(emailVerb != null){
			indexOfVerb = request.indexOf(emailVerb);
			request = request.substring(indexOfVerb);
		}
		return Utils.indexOfWord(request, TO_KEYWORD) + indexOfVerb;
	}
	
	//like in sms responsor
	int contentIndex(String request, StringWrapper outContentKeyword){
		int contentIndex;
		for(String contentWord:SqlData.smsContentWords){
			contentIndex = Utils.lastIndexOfWord(request, contentWord);
			if(contentIndex != -1){
				if(outContentKeyword != null) outContentKeyword.setStr(contentWord);
				return contentIndex;
			}
		}
		return -1;
	}
	
	int titleIndex(String request, StringWrapper outTitleKeyword){
		int titleIndex;
		for(String titleWord:SqlData.emailTitle){
			titleIndex = Utils.lastIndexOfWord(request, titleWord);
			if(titleIndex != -1){
				if(outTitleKeyword != null) outTitleKeyword.setStr(titleWord);
				return titleIndex;
			}
		}
		return -1;
	}
	/**
	 * Check information email address, title, body. If it enough all information, send email. It use 
	 * when have not information
	 * @return string is responsed to user.
	 */
	String checkInfoAndSendIfEnoughAllInfo(){
		if(email != null && content != null && title != null){
			EmailAction emailAction = new EmailAction(context);
			emailAction.send(email, title, content);
			return context.getString(R.string.send_email_command);
		}
		else if(email == null && content == null && title == null){
			relativeLastResponse = RELATIVE_ADD_INFO;
			return Utils.getRandomString(SqlData.emailAddInfo);
		}
		else{
			relativeLastResponse = RELATIVE_ASK_ADD_INFO;
			return Utils.getRandomString(SqlData.emailAskAddInfo);
		}
	}
	
	/**
	 * Check information email address, title, body. If it has one of these informatin, send email. It use 
	 * when have some information
	 * @return string is responsed to user.
	 */
	String checkInfoAndSendIfExistAnyInfo(){
		if(email != null || content != null || title != null){
			EmailAction emailAction = new EmailAction(context);
			emailAction.send(email, title, content);
			relativeLastResponse = RELATIVE_NONE;
			return context.getString(R.string.send_email_command);
		}
		else if(email == null && content == null && title == null){
			relativeLastResponse = RELATIVE_ADD_INFO;
			return Utils.getRandomString(SqlData.emailAddInfo);
		}
		return null;
	}
	
	void parseContentIfHaveNotAnyKeywordOrOnlyHaveContentKeyword(String request){
		if(content == null){
			StringWrapper contentWrapper = new StringWrapper();
			int contentIndex = contentIndex(request, contentWrapper);
			String contentKeyword = contentWrapper.getStr();
			
			int titleIndex = titleIndex(request, null);
			int addressIndex = addressIndex(request, null);
			
			if(addressIndex == -1 && titleIndex == -1){
				if(contentIndex != -1){
					content = request.substring(contentKeyword.length() + 1);
				}
				else content = request;
			}
		}
	}
	
	public void reset(){
		super.reset();
		email = null;
		content = null;
		title = null;
	}
	
	@Override
	public int indexOfMainVerb(String request) {
		StringWrapper emailVerbWrapper = new StringWrapper();
		
		if(Utils.isExistElemInArrayIsWordOfStr(SqlData.emailVerbs, request, emailVerbWrapper))
			return Utils.indexOfWord(request, emailVerbWrapper.getStr());
		return super.indexOfMainVerb(request);
	}
}
